<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="org.edu.system.DBConnect" %>
<%@ page import="java.sql.*" %>
<%
	String tc_id = (String)(session.getAttribute("UserName"));
	String sql = "SELECT * FROM teacher WHERE tc_id = '" + tc_id + "'";
	System.out.println(sql);
	
	Connection con = DBConnect.getCon();
	Statement stmt = null;
	ResultSet rs = null;
	stmt = con.createStatement();
	rs = stmt.executeQuery(sql);//执行命令
	if (rs.next())// 如果记录集非空，表明有匹配的用户ID
	{
			//正常进行
	} else {
		// 否则登录失败
		// response.sendRedirect ( "MyJsp.jsp" );
		response.sendRedirect("login_failure.jsp");
	}
%>
<!DOCTYPE html>
<html>
<head>
<link href="./bootstrap/css/bootstrap.min.css" rel="stylesheet">
<meta charset="utf-8">
<title>教师教务管理系统</title>
</head>
<body style="width:900px; margin:0 auto;">
<%
	sql = "SELECT c.course_name,l.class FROM lesson l,course c WHERE lesson_id = '" + request.getParameter("lessonID") + "' and l.course_id = c.course_id";
	System.out.println(sql);
	stmt = null;
	rs = null;
	stmt = con.createStatement();
	rs = stmt.executeQuery(sql);//执行命令
	if (rs.next())// 如果记录集非空，表明有匹配的用户ID
	{
		out.print("<h4>"+rs.getString("course_name")+"&nbsp;&nbsp;"+rs.getString("class")+"</h4>");
	} 
%>
<center>
<form action="<%=request.getContextPath()%>/SaveGrade" method="post" id="myform">
<table border="1" width="75%" style="text-align:center" class="table table-striped table-bordered">
	<tr>
		<th style="text-align:center">学号</th>
		<th style="text-align:center">姓名</th>
		<th style="text-align:center">成绩</th>
	</tr>
	<% 
		ResultSet rs1 = null;
		sql = "SELECT stu_id,stu_name FROM student WHERE course_list LIKE \"%"+request.getParameter("lessonID")+",%\"";
		stmt = null;
		rs = null;
		stmt = con.createStatement();
		rs = stmt.executeQuery(sql);//执行命令
		while (rs.next())// 如果记录集非空，表明有匹配的用户ID
		{
			String stu_id = rs.getString("stu_id");
			out.print("<tr>");
			out.print("<td>"+rs.getString("stu_id")+"</td>");
			out.print("<td>"+rs.getString("stu_name")+"</td>");
			sql = "select grade from grade where lesson_id = '" + request.getParameter("lessonID") + "' and stu_id='"+stu_id+"'";
			System.out.println(sql);
			stmt = con.createStatement();
			rs1 = stmt.executeQuery(sql);//执行命令
			if(rs1.next())
			{
				out.print("<td>成绩：<input type=\"text\" name=\""+stu_id+"\" value=\""+rs1.getString("grade")+"\"/></td>");
			}else{
				out.print("<td>成绩：<input type=\"text\" name=\""+stu_id+"\" value=\"0\"/></td>");
			}
			out.print("</tr>");
		} 
	%>
</table>
<input type="hidden" name="lesson_id" value="<%=request.getParameter("lessonID")%>"/>
<br>
    <button type="submit" οnclick="alert(1)" class="btn btn-success" id="saveGrade"/>保存成绩</button>
    <input type="button" onclick="mySubmit()" class="btn btn-primary" value="提交成绩">
</form>
</center>
<script>
function mySubmit()
{
	var myform = document.getElementById("myform");
	var myInput = document.createElement('input');
	myInput.type = "hidden";
	myInput.name = "submitGrade";
	myInput.vlaue = "111";
	myform.appendChild(myInput);
	document.getElementById("saveGrade").click();
}
</script>
</body>
</html>